function [ new_vessel_trees,delet_vessel_trees] = MergeTrees( vessel_trees )
%MERGETREES Summary of this function goes here
%   Detailed explanation goes here
new_vessel_trees=vessel_trees;
delet_vessel_trees=[];
for i=1:length(vessel_trees)
    search_level=0;
    search_startpoint=[];
    if vessel_trees(i).element7.segments_num~=0
        search_level=7;
        search_startpoint=vessel_trees(i).element7.segments(1).start(1,:);
    else if vessel_trees(i).element6.segments_num~=0
            search_level=6;
            search_startpoint=vessel_trees(i).element6.segments(1).start(1,:);
        else if vessel_trees(i).element5.segments_num~=0
                search_level=5;
                search_startpoint=vessel_trees(i).element5.segments(1).start(1,:);
            else if vessel_trees(i).element4.segments_num~=0
                    search_level=4;
                    search_startpoint=vessel_trees(i).element4.segments(1).start(1,:);
                else if vessel_trees(i).element3.segments_num~=0
                        search_level=3;
                        search_startpoint=vessel_trees(i).element3.segments(1).start(1,:);
                    end
                end
            end
        end
    end
    for j=1:length(new_vessel_trees)
        temp_search_level=search_level;
        while (1)
            temp=find(delet_vessel_trees==j,1);
            if isempty(temp)==0
                break;
            end
            if temp_search_level>8 || j>=i
                break;
            end
            switch temp_search_level
                case 7
                    for m=1:length(new_vessel_trees(j).element7.segments)
                        for n=1:size(new_vessel_trees(j).element7.segments(m).end,1)
                            if norm(search_startpoint-new_vessel_trees(j).element7.segments(m).end(n,:))<1e-3
                                new_vessel_trees(j).element7.segments=[new_vessel_trees(j).element7.segments,vessel_trees(i).element7.segments];
                                new_vessel_trees(j).element7.segments_num=new_vessel_trees(j).element7.segments_num+vessel_trees(i).element7.segments_num;
                                
                                new_vessel_trees(j).element6.segments=[new_vessel_trees(j).element6.segments,vessel_trees(i).element6.segments];
                                new_vessel_trees(j).element6.segments_num=new_vessel_trees(j).element6.segments_num+vessel_trees(i).element6.segments_num;
                                
                                new_vessel_trees(j).element5.segments=[new_vessel_trees(j).element5.segments,vessel_trees(i).element5.segments];
                                new_vessel_trees(j).element5.segments_num=new_vessel_trees(j).element5.segments_num+vessel_trees(i).element5.segments_num;
                                
                                new_vessel_trees(j).element4.segments=[new_vessel_trees(j).element4.segments,vessel_trees(i).element4.segments];
                                new_vessel_trees(j).element4.segments_num=new_vessel_trees(j).element4.segments_num+vessel_trees(i).element4.segments_num;
                                
                                new_vessel_trees(j).element3.segments=[new_vessel_trees(j).element3.segments,vessel_trees(i).element3.segments];
                                new_vessel_trees(j).element3.segments_num=new_vessel_trees(j).element3.segments_num+vessel_trees(i).element3.segments_num;
                                
                                new_vessel_trees(j).element2.segments=[new_vessel_trees(j).element2.segments,vessel_trees(i).element2.segments];
                                new_vessel_trees(j).element2.segments_num=new_vessel_trees(j).element2.segments_num+vessel_trees(i).element2.segments_num;
                                
                                new_vessel_trees(j).element1.segments=[new_vessel_trees(j).element1.segments,vessel_trees(i).element1.segments];
                                new_vessel_trees(j).element1.segments_num=new_vessel_trees(j).element1.segments_num+vessel_trees(i).element1.segments_num;
                                
                                delet_vessel_trees=[delet_vessel_trees,i];
                            end
                        end
                    end
                case 6
                    for m=1:length(new_vessel_trees(j).element6.segments)
                        for n=1:size(new_vessel_trees(j).element6.segments(m).end,1)
                            if norm(search_startpoint-new_vessel_trees(j).element6.segments(m).end(n,:))<1e-3
                                new_vessel_trees(j).element6.segments=[new_vessel_trees(j).element6.segments,vessel_trees(i).element6.segments];
                                new_vessel_trees(j).element6.segments_num=new_vessel_trees(j).element6.segments_num+vessel_trees(i).element6.segments_num;
                                
                                new_vessel_trees(j).element5.segments=[new_vessel_trees(j).element5.segments,vessel_trees(i).element5.segments];
                                new_vessel_trees(j).element5.segments_num=new_vessel_trees(j).element5.segments_num+vessel_trees(i).element5.segments_num;
                                
                                new_vessel_trees(j).element4.segments=[new_vessel_trees(j).element4.segments,vessel_trees(i).element4.segments];
                                new_vessel_trees(j).element4.segments_num=new_vessel_trees(j).element4.segments_num+vessel_trees(i).element4.segments_num;
                                
                                new_vessel_trees(j).element3.segments=[new_vessel_trees(j).element3.segments,vessel_trees(i).element3.segments];
                                new_vessel_trees(j).element3.segments_num=new_vessel_trees(j).element3.segments_num+vessel_trees(i).element3.segments_num;
                                
                                new_vessel_trees(j).element2.segments=[new_vessel_trees(j).element2.segments,vessel_trees(i).element2.segments];
                                new_vessel_trees(j).element2.segments_num=new_vessel_trees(j).element2.segments_num+vessel_trees(i).element2.segments_num;
                                
                                new_vessel_trees(j).element1.segments=[new_vessel_trees(j).element1.segments,vessel_trees(i).element1.segments];
                                new_vessel_trees(j).element1.segments_num=new_vessel_trees(j).element1.segments_num+vessel_trees(i).element1.segments_num;
                                
                                delet_vessel_trees=[delet_vessel_trees,i];
                            end
                        end
                    end
                case 5
                    for m=1:length(new_vessel_trees(j).element5.segments)
                        for n=1:size(new_vessel_trees(j).element5.segments(m).end,1)
                            if norm(search_startpoint-new_vessel_trees(j).element5.segments(m).end(n,:))<1e-3
                                new_vessel_trees(j).element5.segments=[new_vessel_trees(j).element5.segments,vessel_trees(i).element5.segments];
                                new_vessel_trees(j).element5.segments_num=new_vessel_trees(j).element5.segments_num+vessel_trees(i).element5.segments_num;
                                
                                new_vessel_trees(j).element4.segments=[new_vessel_trees(j).element4.segments,vessel_trees(i).element4.segments];
                                new_vessel_trees(j).element4.segments_num=new_vessel_trees(j).element4.segments_num+vessel_trees(i).element4.segments_num;
                                
                                new_vessel_trees(j).element3.segments=[new_vessel_trees(j).element3.segments,vessel_trees(i).element3.segments];
                                new_vessel_trees(j).element3.segments_num=new_vessel_trees(j).element3.segments_num+vessel_trees(i).element3.segments_num;
                                
                                new_vessel_trees(j).element2.segments=[new_vessel_trees(j).element2.segments,vessel_trees(i).element2.segments];
                                new_vessel_trees(j).element2.segments_num=new_vessel_trees(j).element2.segments_num+vessel_trees(i).element2.segments_num;
                                
                                new_vessel_trees(j).element1.segments=[new_vessel_trees(j).element1.segments,vessel_trees(i).element1.segments];
                                new_vessel_trees(j).element1.segments_num=new_vessel_trees(j).element1.segments_num+vessel_trees(i).element1.segments_num;
                                
                                delet_vessel_trees=[delet_vessel_trees,i];
                            end
                        end
                    end  
                case 4
                    for m=1:length(new_vessel_trees(j).element4.segments)
                        for n=1:size(new_vessel_trees(j).element4.segments(m).end,1)
                            if norm(search_startpoint-new_vessel_trees(j).element4.segments(m).end(n,:))<1e-3
                                
                                new_vessel_trees(j).element4.segments=[new_vessel_trees(j).element4.segments,vessel_trees(i).element4.segments];
                                new_vessel_trees(j).element4.segments_num=new_vessel_trees(j).element4.segments_num+vessel_trees(i).element4.segments_num;
                                
                                new_vessel_trees(j).element3.segments=[new_vessel_trees(j).element3.segments,vessel_trees(i).element3.segments];
                                new_vessel_trees(j).element3.segments_num=new_vessel_trees(j).element3.segments_num+vessel_trees(i).element3.segments_num;
                                
                                new_vessel_trees(j).element2.segments=[new_vessel_trees(j).element2.segments,vessel_trees(i).element2.segments];
                                new_vessel_trees(j).element2.segments_num=new_vessel_trees(j).element2.segments_num+vessel_trees(i).element2.segments_num;
                                
                                new_vessel_trees(j).element1.segments=[new_vessel_trees(j).element1.segments,vessel_trees(i).element1.segments];
                                new_vessel_trees(j).element1.segments_num=new_vessel_trees(j).element1.segments_num+vessel_trees(i).element1.segments_num;
                                
                                delet_vessel_trees=[delet_vessel_trees,i];
                            end
                        end
                    end
            end
            temp_search_level=temp_search_level+1;
        end
    end           
end
new_vessel_trees(delet_vessel_trees)=[];
end

